//
//  FPMyCustomerController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/24.
//

#import "FPMyCustomerController.h"
#import "FPMyCustomerCell.h"
#import "FPCustomerModel.h"


@interface FPMyCustomerController () <UITableViewDelegate , UITableViewDataSource>



@end

@implementation FPMyCustomerController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}


- (void)initUI {
    
    self.view.backgroundColor = [UIColor hexColor:@"#F4F6FA"];
    
    [self setTitle:@"我的客服" style:(XYCustomNavigationStyleWhite) hiddenBackBtn:NO];
    
    [self.view addSubview:self.tableView];
    self.tableView.frame = CGRectMake(0.f, UIDevice.navigationFullHeight, SCREEN_WIDTH, SCREEN_HEIGHT - UIDevice.navigationFullHeight);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = UIColor.whiteColor;
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, SCREEN_WIDTH, 8.f)];
    v.backgroundColor = [UIColor hexColor:@"#F4F6FA"];
    self.tableView.tableHeaderView = v;
    
    self.tableView.rowHeight = 75.f;
    
    self.tableViewScrollEnable = NO;
    
    [self getData];
}

- (void)getData {
    
    [MBManager showLoading];
    [FPCustomerModel getCustomersSuccess:^(NSInteger code, NSString * _Nonnull message, NSArray * _Nonnull list) {
        [MBManager hideLoading];
        
        [self.dataSource removeAllObjects];
        for (FPCustomerModel *model in list) {
            
            [self.dataSource addObject:model];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        [self showError:error];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    FPMyCustomerCell *cell;
    
    if (cell == nil) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPMyCustomerCell.class) owner:nil options:nil].lastObject;
    }
    
//    if (indexPath.row == self.dataSource.count - 1) {
//        
//        cell.lineView.hidden = YES;
//    }
    
    FPCustomerModel *model = self.dataSource[indexPath.row];
    
    cell.titleLab.text = model.contactType;
    cell.subTitleLab.text = model.contactValue;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPCustomerModel *model = self.dataSource[indexPath.row];
    
    if ([model.type isEqualToString:@"2"]) {
        
        NSString *telURLString = [NSString stringWithFormat:@"tel://%@", model.contactValue];
        NSURL *telURL = [NSURL URLWithString:telURLString];
        
        // 确保 URL 可以被打开
        if ([[UIApplication sharedApplication] canOpenURL:telURL]) {
            
            [[UIApplication sharedApplication] openURL:telURL options:@{} completionHandler:nil];
        }
        else {
            NSLog(@"无法打开电话拨号界面");
        }
    }
    else {
        
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = model.contactValue;
        [self showText:@"已复制"];
    }
}


@end
